const vm = new Vue({
    el: '#app',
    data: {
        tableData: [],//表格当前页数据
        select: '',//下拉菜单选项
        pagesize: 20,//默认每页数据量
        curRowData: {}, //当前选择行数据
        currentPage: 1,//当前页码
        start: 1, //查询的页码
        totalCount: '',//默认数据总数
        multipleSelection: [],//多选数组
        dialogFormVisible:false,//编辑框
        q:{//搜索条件
            exchangeCode:'',
            userId:'',
            used:'',
            beginTime:'',
            endTime:''
        },

        couponList: [],//优惠券列表
        couponIds:[],
        count:'',
        checkbtn:false,
        loading:false,
    },
    created () {
        Vue.nextTick(()=>{
            //载入数据
            vm.loadData(vm.currentPage, vm.pagesize);
        });
    },
    methods: {
        //从后台获取数据
        loadData: function (page, limit) {
            let {exchangeCode,userId,used,beginTime,endTime} = vm.q
            if (beginTime) {
                // const date = new Date(beginTime);
                // beginTime=date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + ' ' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds();
                beginTime=vm.dateFormat(new Date(beginTime))
            }
            if (endTime) {
                // const date = new Date(endTime);
                // endTime=date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + ' ' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds();
                endTime=vm.dateFormat(new Date(endTime))
            }
            const params = {page,limit,exchangeCode,userId,used,beginTime,endTime};
            $.ajax({
                type: "POST",
                url: baseURL + "promotion/usercouponexchange/list",
                contentType: "application/json",
                data: JSON.stringify(params),
                success: function(r){
                    if(r.code === 0){
                        vm.tableData = r.page.list
                        vm.totalCount = r.page.totalCount
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        //多选响应
        handleSelectionChange: function(val) {
            vm.multipleSelection = val;
        },
        //点击行响应
        handleclick: function(row, event, column){
            vm.curRowData = JSON.parse(JSON.stringify(row));//深克隆
        },
        //弹出编辑框
        handleEdit: function(index, row) {
            vm.curRowData = JSON.parse(JSON.stringify(row));//深克隆
            vm.dialogFormVisible = true
        },
        //提交编辑
        edit() {
            $.ajax({
                type: "POST",
                url: baseURL + "userTeam/userteam/update",
                contentType: "application/json",
                data: JSON.stringify(vm.curRowData),
                success: function(r){
                    if(r.code === 0){
                        vm.loadData(vm.currentPage, vm.pagesize);//刷新后页面在当前页
                        //vm.loadData(vm.currentPage=1, vm.pagesize);刷新后页面回到首页
                        vm.dialogFormVisible = false
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },

        //单行删除
        handleDelete: function(index, row) {
            $.ajax({
                type: "POST",
                url: baseURL + "userTeam/userteam/delete",
                contentType: "application/json",
                data: JSON.stringify(row),
                success: function(r){
                    if(r.code === 0){
                        alert('删除成功')
                        vm.loadData(vm.currentPage, vm.pagesize);//刷新后页面在当前页
                        //vm.loadData(vm.currentPage=1, vm.pagesize);刷新后页面回到首页
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        //搜索
        search: function(){
            vm.loadData(vm.start, vm.pagesize);
        },
        //重置
        rest() {
            vm.q.exchangeCode=''
            vm.q.userId=''
            vm.q.used=''
            vm.q.beginTime=''
            vm.q.endTime=''
            vm.loadData(vm.currentPage, vm.pagesize);
        },
        add() {

        },
        //每页显示数据量变更
        handleSizeChange: function(val) {
            vm.pagesize = val;
            vm.loadData(vm.currentPage, vm.pagesize);
        },
        //页码变更
        handleCurrentChange: function(val) {
            vm.currentPage = val;
            vm.loadData(vm.currentPage, vm.pagesize);
        },
        createCodeView() {
            vm.couponIds=[]
            vm.count=''
            $.get(baseURL + "promotion/configcouponucenter/getCouponList", function (r) {
                vm.couponList = r.couponList
            });
            vm.dialogFormVisible=true
        },
        createCode() {
            if (vm.count <= 0) {
                alert('请输入大于0的数字')
                return
            }
            if (vm.count > 500) {
                alert('单次生成数量不能大于500!')
                return
            }
            vm.checkbtn=true
            vm.loading=true
            $.ajax({
                type: "POST",
                url: baseURL + "promotion/usercouponexchange/createCode",
                contentType: "application/json",
                data: JSON.stringify({couponIds:vm.couponIds.join(','),count:vm.count}),
                success: function(r){
                    if(r.code === 0){
                        vm.loadData(vm.currentPage, vm.pagesize);
                        vm.dialogFormVisible=false
                        vm.checkbtn=false
                        vm.loading=false
                        alert('兑换码生成成功')
                    }else{
                        vm.checkbtn=false
                        vm.loading=false
                        alert(r.msg);
                    }
                }
            });
        },
        usedFormatter(val) {
            if(val.used == 0)
            {return "未使用"}
            else if (val.used == 1)
            {return "已使用"}
            else
            {return ""}
        },
        disabledFormatter(val) {
            if(val.disabled == 0)
            {return "未失效"}
            else if (val.disabled == 1)
            {return "已失效"}
            else
            {return ""}
        },
        deletes() {
            if (vm.multipleSelection.length <= 0) {
                alert('至少选择一项!')
                return
            }
            vm.multipleSelection.forEach(v=>{
                if (v.used == 1) {
                    alert('只能删除未使用的兑换码!')
                    return
                }
            });
            $.ajax({
                type: "POST",
                url: baseURL + "promotion/usercouponexchange/deletes",
                contentType: "application/json",
                data: JSON.stringify(vm.multipleSelection),
                success: function(r){
                    if(r.code === 0){
                        vm.loadData(vm.currentPage, vm.pagesize);
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        /* 格式化日期yyyy-MM-dd HH:mm:ss */
        dateFormat (date) {
            var year = date.getFullYear();
            var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
            var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
            var hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
            var minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
            var seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
            return year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
        },
    }
});
